unit nomenclaturecreateedit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Buttons,
  StdCtrls, DbCtrls, ExtCtrls, ComCtrls, ExtDlgs, db,ZDataset;

type

  { TfrmNomenclatureCreateEdit }

  TfrmNomenclatureCreateEdit = class(TForm)
    btnDeleteImage: TBitBtn;
    btnAddImage: TBitBtn;
    btnSave: TBitBtn;
    btnCancel: TBitBtn;
    btnGroupSelect: TBitBtn;
    DBEdtTitle: TDBEdit;
    DBEdtQuantityMin: TDBEdit;
    DBEdtArticle: TDBEdit;
    DBEdtArticleInside: TDBEdit;
    DBEditAllQuantity: TDBEdit;
    DBEditPrice: TDBEdit;
    DBEdtPriceSmallWholeSale: TDBEdit;
    DBEdtPriceBigWholeSale: TDBEdit;
    DBEdtPriceRetail: TDBEdit;
    dbImage: TDBImage;
    DBNavigatorPictures: TDBNavigator;
    edtGroup: TEdit;
    gbPhoto: TGroupBox;
    lblTitle: TLabel;
    lblPriceRetail: TLabel;
    lblImageList: TLabel;
    lblMinQuantity: TLabel;
    lblGroup: TLabel;
    lblArticle: TLabel;
    lblArticleInside: TLabel;
    lblAllQuantity: TLabel;
    lblPrice: TLabel;
    lblPriceSmallWholeSale: TLabel;
    lblPriceBigWholeSale: TLabel;
    ListBoxImages: TListBox;
    OpenDialog1: TOpenDialog;
    OpenPictureDialog: TOpenPictureDialog;
    procedure btnAddImageClick(Sender: TObject);
    procedure btnDeleteImageClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure btnGroupSelectClick(Sender: TObject);
    procedure DBEditPriceKeyPress(Sender: TObject; var Key: char);
    procedure DBEdtQuantityMinChange(Sender: TObject);
    procedure DBEdtPriceSmallWholeSaleChange(Sender: TObject);
    procedure DBEdtPriceBigWholeSaleChange(Sender: TObject);
    procedure DBEdtPriceRetailChange(Sender: TObject);
    procedure DBEditPriceChange(Sender: TObject);
    procedure DBEdtTitleChange(Sender: TObject);
    procedure DBNavigatorPicturesClick(Sender: TObject; Button: TDBNavButtonType);
    procedure edtGroupChange(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
    procedure checkErrors;
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  frmNomenclatureCreateEdit: TfrmNomenclatureCreateEdit;

implementation

{$R *.lfm}
uses main,nomenclature,groupselect , FPimage,FPReadJPEG,FPWriteJPEG;

{ TfrmNomenclatureCreateEdit }

procedure TfrmNomenclatureCreateEdit.checkErrors;
begin
  if ((DBEdtTitle.Text=EmptyStr) OR (DBEditPrice.Text=EmptyStr) OR (DBEdtPriceRetail.Text=EmptyStr) OR (DBEdtPriceSmallWholeSale.Text=EmptyStr) OR (DBEdtPriceBigWholeSale.Text=EmptyStr) OR (DBEdtQuantityMin.Text=EmptyStr)) then begin
    btnSave.Enabled:=False;
  end else begin
    btnSave.Enabled:=True;
  end;
  if ListBoxImages.Count>0 then begin
    btnDeleteImage.Enabled:=True;
  end else begin
    btnDeleteImage.Enabled:=False;
  end;
  if DBEdtTitle.DataSource.DataSet.State = dsInsert then begin
    btnAddImage.Enabled:=False;
  end else begin
    btnAddImage.Enabled:=True;
  end;
end;

procedure TfrmNomenclatureCreateEdit.FormShow(Sender: TObject);
{var
  query: TZQuery;
  i:Integer; }
begin
{  ListBoxImages.Clear;
  dbImage.Picture.Clear;
  if DBEdtTitle.DataSource.DataSet.State <> dsInsert then begin
    query := TZQuery.Create(nil);
    query.Connection := frmMain.ZConnection;
    query.SQL.Text := 'SELECT id FROM pictures WHERE nomenclatureid='+DBEdtTitle.DataSource.DataSet.FieldByName('id').AsString;
    dbImage.DataSource := query.DataSource;
    dbImage.DataField := 'data';
    query.Open;
    query.First;
    for i:=1 to query.RecordCount do begin
      ListBoxImages.Items.Add(query.FieldByName('id').AsString);
      query.Next;
    end;
    query.Free;
  end;}
  if DBEdtTitle.DataSource.DataSet.State <> dsInsert then begin
     frmMain.ZTablePictures.Filter := 'nomenclatureid=' + DBEdtTitle.DataSource.DataSet.FieldByName('id').AsString;
     frmMain.ZTablePictures.Filtered := True;
     frmMain.ZTablePictures.First;
     DBNavigatorPictures.Enabled := True;
  end else begin
     dbImage.Picture.Clear;
     DBNavigatorPictures.Enabled := False;
  end;
  checkErrors;
end;

procedure TfrmNomenclatureCreateEdit.btnSaveClick(Sender: TObject);
begin
  FrmMain.DSNomenclature.DataSet.Edit;
  if (edtGroup.Text='') then begin
     frmNomenclature.CurrentGroupRecordNumber:=0;
  end;
  { TODO : Виправити помилку створення номенклатури в різних вікнах }
  FrmMain.DSNomenclature.DataSet.FieldByName('parentid').AsInteger:=frmNomenclature.CurrentGroupRecordNumber;
  FrmMain.DSNomenclature.DataSet.FieldByName('isgroup').AsBoolean:=False;
  FrmMain.DSNomenclature.DataSet.Post;
  FrmMain.DSNomenclature.DataSet.Refresh;
  Close;
end;

procedure TfrmNomenclatureCreateEdit.btnAddImageClick(Sender: TObject);
{var
  query : TZQuery;
  mdbImage : TDBImage; }
begin
{  if OpenPictureDialog.Execute then begin
    //17000 b = 17 kb
    if FileSize(OpenPictureDialog.FileName)<=17000 then begin
      frmMain.ZTablePictures.Append;
      frmMain.ZTablePictures.FieldByName('nomenclatureid').AsString:=DBEdtTitle.DataSource.DataSet.FieldByName('id').AsString;
      dbImage.Picture.LoadFromFile(OpenPictureDialog.FileName);

      {query := TZQuery.Create(nil);
      mdbImage := TDBImage.Create(Self);
      query.Connection := frmMain.ZConnection;
      query.SQL.Text := 'SELECT * FROM pictures WHERE id=1';
      query.Open;
      query.Append;
      query.FieldByName('nomenclatureid').AsString:=DBEdtTitle.DataSource.DataSet.FieldByName('id').AsString;
      }

      //mdbImage.Parent := Self;
      //mdbImage.DataSource := query.DataSource;
      //mdbImage.DataField := 'data';
      //mdbImage.Picture.LoadFromFile(OpenPictureDialog.FileName);
      //query.Post;
      ListBoxImages.Items.Add(query.FieldByName('id').AsString);
      //query.Free;
    end else begin
      ShowMessage('Фото повинне мати розмір не більше 17 кілобайт.'+#13+'Розмір фото становить: '+FloatToStr(FileSize(OpenPictureDialog.FileName)/1024)+' кб.');
    end;
  end;   }
  { for future optimizing/compress
  var img:TFPMemoryImage;
       jpw: TFPWriterJPEG;
       jpr:TFPReaderJPEG;
  begin
   img:=TFPMemoryImage.Create(0,0);
   jpr:=TFPReaderJPEG.Create;
   jpw:=TFPWriterJPEG.Create;

   img.LoadFromFile('s.jpg',jpr);
   jpw.CompressionQuality:=15;
   img.SaveToFile('1.jpeg',jpw);

   jpr.free;
   jpw.free;
   img.free;}
end;

procedure TfrmNomenclatureCreateEdit.btnDeleteImageClick(Sender: TObject);
begin
  {if ListBoxImages.Count>0 then begin
    frmMain.execQuery('DELETE FROM pictures WHERE id='+ListBoxImages.Items.Strings[ListBoxImages.ItemIndex]);
    ListBoxImages.Items.Delete(ListBoxImages.ItemIndex);
    dbImage.Picture.Clear;
  end;}
end;

procedure TfrmNomenclatureCreateEdit.btnCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmNomenclatureCreateEdit.btnGroupSelectClick(Sender: TObject);
begin
  frmGroupSelect.groupsId:=1;
  frmGroupSelect.ShowModal;
  if frmGroupSelect.selectedGroupId<>EmptyStr then begin
    frmNomenclature.CurrentGroupRecordNumber:=StrToInt(frmGroupSelect.selectedGroupId);
    edtGroup.Text:=frmGroupSelect.selectedGroupTitle;
  end;
end;

procedure TfrmNomenclatureCreateEdit.DBEditPriceKeyPress(Sender: TObject;
  var Key: char);
begin
  FrmMain.KeyVerifyFloat(Sender,Key);
end;

procedure TfrmNomenclatureCreateEdit.DBEdtQuantityMinChange(Sender: TObject);
begin
  checkErrors;
end;

procedure TfrmNomenclatureCreateEdit.DBEdtPriceSmallWholeSaleChange(Sender: TObject);
begin
  checkErrors;
end;

procedure TfrmNomenclatureCreateEdit.DBEdtPriceBigWholeSaleChange(Sender: TObject);
begin
  checkErrors;
end;

procedure TfrmNomenclatureCreateEdit.DBEdtPriceRetailChange(Sender: TObject);
begin
  checkErrors;
end;

procedure TfrmNomenclatureCreateEdit.DBEditPriceChange(Sender: TObject);
begin
  checkErrors;
end;

procedure TfrmNomenclatureCreateEdit.DBEdtTitleChange(Sender: TObject);
begin
  checkErrors;
end;

procedure TfrmNomenclatureCreateEdit.DBNavigatorPicturesClick(Sender: TObject;
  Button: TDBNavButtonType);
begin
  if ((Button = nbInsert) OR (Button = nbEdit)) AND OpenDialog1.Execute then begin
    //20000 b = 20 kb
    if FileSize(OpenDialog1.FileName)<=20000 then begin
      frmMain.ZTablePictures.FieldByName('nomenclatureid').AsString:=DBEdtTitle.DataSource.DataSet.FieldByName('id').AsString;
      dbImage.Picture.LoadFromFile(OpenDialog1.FileName);
    end else begin
      frmMain.ZTablePictures.Cancel;
      ShowMessage('Фото повинне мати розмір не більше 20 кілобайт.'+#13+'Розмір фото становить: '+FloatToStr(FileSize(OpenPictureDialog.FileName)/1024)+' кб.');
    end;
  end else begin
    frmMain.ZTablePictures.Cancel;
  end;
end;

procedure TfrmNomenclatureCreateEdit.edtGroupChange(Sender: TObject);
begin
  checkErrors;
end;

procedure TfrmNomenclatureCreateEdit.FormCloseQuery(Sender: TObject;
  var CanClose: boolean);
begin
  FrmMain.DSNomenclature.DataSet.Cancel;
  FrmMain.DSNomenclature.DataSet.Refresh;
end;

end.

